/**
 * 
 */
package slhs.tempo.pivotTableUtilities;

import java.awt.Component;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;

import slhs.tempo.TableAnalyzer.SaveCurrentState;
import slhs.tempo.TableAnalyzer.TablePanelHandler;
import slhs.tempo.scriptGenerator.PanelHandler;

import com.jgoodies.forms.factories.DefaultComponentFactory;

/**
 * PivotTableParametersConfigPanelHandler.java
 * 
 * @author Swapnil Devikar
 * 
 */
public class PivotTableParametersConfigPanelHandler extends JPanel implements PanelHandler {
    private static final String PIVOT_TABLE_PARMETERS_LAYOUT = "PIVOT_TABLE_PARAMETERS_PANEL_HANDLER_LAYOUT.XML";

    private JPanel pivotTableControlsPanel;
    private static TablePanelHandler tablePanelHandlerObject;
    private static JComboBox tablePanelTabsListComboBox;

    private Component selectColumnFieldsSeperator;
    private Component selectRowFieldsSeperator;

    private JButton addColumnFieldButton;
    private JButton updatePivotTableButton;
    private JButton addRowFieldButton;
    private JButton removeRowFieldButton;
    private JButton removeColumnFieldButton;

    private JLabel changeInputTableLabel;

    private static JList availableRowFieldsList;
    private static DefaultListModel availableRowFieldsListModel;
    private static JList availableColumnFieldsList;
    private static DefaultListModel availableColumnFieldsListModel;

    private JList pivotTableRowsFieldsList;
    private DefaultListModel pivotTableRowFieldsListModel;
    private JList pivotTableColumnsFieldsList;
    private DefaultListModel pivotTableColumnFieldsListModel;

    private JScrollPane availableRowsFieldListScrollPane;
    private JScrollPane pivotTableRowsFieldsListScrollPane;
    private JScrollPane pivotTableColumnsFieldsListScrollPane;
    private JScrollPane availableColumnFieldsListScrollPane;

    public PivotTableParametersConfigPanelHandler(TablePanelHandler tablePanelHandler) {
	tablePanelHandlerObject = tablePanelHandler;
	initComponents();
	addComponents();

    }

    /**
     * 
     */
    private void addComponents() {
	// here is where we load the layout constraints. change the xml filename!!!
	org.mlc.swing.layout.LayoutConstraintsManager layoutConstraintsManager = org.mlc.swing.layout.LayoutConstraintsManager
		.getLayoutConstraintsManager(this.getClass().getResourceAsStream(PIVOT_TABLE_PARMETERS_LAYOUT));
	LayoutManager panelLayout = layoutConstraintsManager.createLayout("panel", this);
	pivotTableControlsPanel.setLayout(panelLayout);

	// here we add the controls to the container.
	pivotTableControlsPanel.add(addColumnFieldButton, "addColumnFieldButton");
	pivotTableControlsPanel.add(changeInputTableLabel, "changeInputTableLabel");
	pivotTableControlsPanel.add(updatePivotTableButton, "updatePivotTableButton");
	pivotTableControlsPanel.add(availableColumnFieldsListScrollPane, "availableColumnFieldsList");
	pivotTableControlsPanel.add(pivotTableColumnsFieldsListScrollPane, "pivotTableColumnsFieldsList");
	pivotTableControlsPanel.add(addRowFieldButton, "addRowFieldButton");
	pivotTableControlsPanel.add(tablePanelTabsListComboBox, "tablePanelTabsListComboBox");
	pivotTableControlsPanel.add(pivotTableRowsFieldsListScrollPane, "pivotTableRowsFieldsList");
	pivotTableControlsPanel.add(availableRowsFieldListScrollPane, "availableRowsFieldList");
	pivotTableControlsPanel.add(removeRowFieldButton, "removeRowFieldButton");
	pivotTableControlsPanel.add(removeColumnFieldButton, "removeColumnFieldButton");
	pivotTableControlsPanel.add(selectColumnFieldsSeperator, "selectColumnFieldsSeperator");
	pivotTableControlsPanel.add(selectRowFieldsSeperator, "selectRowFieldsSeperator");

	// control configuration
	addColumnFieldButton.addActionListener(this);
	addRowFieldButton.addActionListener(this);
	removeColumnFieldButton.addActionListener(this);
	removeRowFieldButton.addActionListener(this);
	updatePivotTableButton.addActionListener(this);

    }

    /**
     * 
     */
    private void initComponents() {

	pivotTableControlsPanel = new JPanel();

	tablePanelTabsListComboBox = new JComboBox();

	addColumnFieldButton = new JButton("Add>>");
	updatePivotTableButton = new JButton("Create/Update Table");
	addRowFieldButton = new JButton("Add>>");
	removeRowFieldButton = new JButton("<<Remove");
	removeColumnFieldButton = new JButton("<<Remove");

	changeInputTableLabel = new JLabel("Change Table:");

	availableRowFieldsList = new JList();
	availableRowFieldsListModel = new DefaultListModel();
	availableColumnFieldsList = new JList();
	availableColumnFieldsListModel = new DefaultListModel();
	pivotTableRowsFieldsList = new JList();
	pivotTableRowFieldsListModel = new DefaultListModel();
	pivotTableColumnsFieldsList = new JList();
	pivotTableColumnFieldsListModel = new DefaultListModel();

	availableRowsFieldListScrollPane = new JScrollPane(availableRowFieldsList);
	pivotTableRowsFieldsListScrollPane = new JScrollPane(pivotTableRowsFieldsList);
	pivotTableColumnsFieldsListScrollPane = new JScrollPane(pivotTableColumnsFieldsList);
	availableColumnFieldsListScrollPane = new JScrollPane(availableColumnFieldsList);

	selectColumnFieldsSeperator = DefaultComponentFactory.getInstance().createSeparator("Column Fields",
		SwingConstants.LEFT);
	selectRowFieldsSeperator = DefaultComponentFactory.getInstance().createSeparator("Row Fields",
		SwingConstants.LEFT);

    }

    public static void updateTableList(String[] tableList) {
	tablePanelTabsListComboBox.removeAllItems();
	for (String tableName : tableList) {
	    tablePanelTabsListComboBox.addItem(tableName);
	}
    }

    public static void updateAvailableRowList(String[] availableRowNames) {
	availableRowFieldsList.removeAll();
	availableRowFieldsListModel.clear();
	for (String columnName : availableRowNames)
	    availableRowFieldsListModel.addElement(columnName);
	availableRowFieldsList.setModel(availableRowFieldsListModel);
    }

    public static void updateAvailableColumnsList(String[] availableColumnNames) {
	availableColumnFieldsList.removeAll();
	availableColumnFieldsListModel.clear();
	for (String columnName : availableColumnNames)
	    availableColumnFieldsListModel.addElement(columnName);
	availableColumnFieldsList.setModel(availableColumnFieldsListModel);
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    @Override
    public void actionPerformed(ActionEvent event) {
	Object eventSource = event.getSource();

	if (eventSource.equals(this.addColumnFieldButton)) {
	    Object[] valuesToAdd = this.availableColumnFieldsList.getSelectedValues();
	    for (Object selectedValue : valuesToAdd) {
		this.pivotTableColumnFieldsListModel.addElement(selectedValue);
		availableColumnFieldsListModel.removeElement(selectedValue);
	    }
	    this.pivotTableColumnsFieldsList.setModel(pivotTableColumnFieldsListModel);
	    availableColumnFieldsList.setModel(availableColumnFieldsListModel);
	}

	if (eventSource.equals(this.removeColumnFieldButton)) {
	    Object[] valuesToRemove = this.pivotTableColumnsFieldsList.getSelectedValues();
	    for (Object selectedValue : valuesToRemove) {
		availableColumnFieldsListModel.addElement(selectedValue);
		this.pivotTableColumnFieldsListModel.removeElement(selectedValue);
	    }
	    pivotTableColumnsFieldsList.setModel(pivotTableColumnFieldsListModel);
	    availableColumnFieldsList.setModel(availableColumnFieldsListModel);
	}

	if (eventSource.equals(this.addRowFieldButton)) {
	    System.out.println("Adding row fields");
	    Object[] valuesToAdd = availableRowFieldsList.getSelectedValues();
	    for (Object selectedValue : valuesToAdd) {
		this.pivotTableRowFieldsListModel.addElement(selectedValue);
		availableRowFieldsListModel.removeElement(selectedValue);
	    }
	    this.pivotTableRowsFieldsList.setModel(pivotTableRowFieldsListModel);
	    availableRowFieldsList.setModel(availableRowFieldsListModel);
	}

	if (eventSource.equals(this.removeRowFieldButton)) {
	    Object[] valuesToRemove = this.pivotTableRowsFieldsList.getSelectedValues();
	    for (Object selectedValue : valuesToRemove) {
		availableRowFieldsListModel.addElement(selectedValue);
		this.pivotTableRowFieldsListModel.removeElement(selectedValue);
	    }
	    pivotTableRowsFieldsList.setModel(pivotTableRowFieldsListModel);
	    availableRowFieldsList.setModel(availableRowFieldsListModel);
	}

	if (eventSource.equals(this.updatePivotTableButton)) {
	    Object[] rowFields = new Object[pivotTableRowFieldsListModel.getSize()];
	    this.pivotTableRowFieldsListModel.copyInto(rowFields);
	    Object[] columnFields = new Object[pivotTableColumnFieldsListModel.getSize()];
	    this.pivotTableColumnFieldsListModel.copyInto(columnFields);

	    // get the selected table
	    int selectedTab = tablePanelHandlerObject.getSelectedTabIndex();
	    JTable selectedTable = SaveCurrentState.tableNameObjectHashmap.get(new Integer(selectedTab));

	    // get row fields
	    Object[] rowFieldCategories = pivotTableRowFieldsListModel.toArray();

	    // get column fields
	    Object[] columnFieldCategories = pivotTableColumnFieldsListModel.toArray();

	    // create a data fields
	    Object[] dataFields = { "Reaction Time" };

	   /* PivotTableFrameHandler.configurePivotTable(selectedTable, rowFieldCategories, columnFieldCategories,
		    dataFields);*/
	}

    }

    /*
     * (non-Javadoc)
     * 
     * @see slhs.tempo.scriptGenerator.PanelHandler#getPanel()
     */
    @Override
    public JPanel getPanel() {
	// TODO Auto-generated method stub
	// pivotTableControlsPanel.setPreferredSize(new Dimension(200, 800));
	return pivotTableControlsPanel;
    }
}
